package vn.pdg.zoombie.dao;

import java.io.File;
import android.content.Context;

public class FileCache {
    
    private File cacheDir;
    
    public FileCache(Context context){
        //Find the dir to save cached images
        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
            cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"/Android/data/.vn.pdg.zoombie/thumb");
        else
            cacheDir=context.getCacheDir();
        if(!cacheDir.exists())
            cacheDir.mkdirs();
    }
    
    public File getFile(String url){
        //I identify images by hashcode. Not a perfect solution, good for the demo.
        String filename=String.valueOf(url.hashCode());
        File f = new File(cacheDir, filename);
        return f;
        
    }
    public void renameFile(String oldUrl,String newUrl){
    	String oldName=String.valueOf(oldUrl.hashCode());
    	String newName=String.valueOf(newUrl.hashCode());
    	File oldFile=new File(cacheDir,oldName);
    	if(oldFile.exists()) oldFile.renameTo(new File(cacheDir, newName));
    }
    
    /**
     * 
     * @Author :dieuddv
     * @Date :Dec 21, 2011
     * TODO: clearr all thumbs
     * void
     */
    public void clear(){
        File[] files=cacheDir.listFiles();
        for(File f:files)
            f.delete();
    }
    /**
     * 
     * @Author :dieuddv
     * @Date :Dec 21, 2011
     * @param url : of file root image
     * TODO: to remove thumbs of root image
     * void
     */
    public void removeFile(String url){
    	String filename=String.valueOf(url.hashCode());
        File f = new File(cacheDir, filename);
        if(f.exists()) f.delete();
    }

}